package com.bestcem.xm.component.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @ClassName: MybatisPlusMetaObjectHandler
 * @Description: 自动填充功能
 * @Author: chenglong.yue
 * @Date: 2022/7/21 16:52
 */
@Slf4j
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("mybatis plus start insert fill ....");
        LocalDateTime now = LocalDateTime.now();
        //this.strictInsertFill(metaObject, "createdDt", () -> now, LocalDateTime.class);
        //this.strictInsertFill(metaObject, "updatedDt", () -> now, LocalDateTime.class);
        //this.strictInsertFill(metaObject, "deleted", () -> Boolean.FALSE, Boolean.class);
        //this.strictInsertFill(metaObject, "versionId", () -> Integer.valueOf(1), Integer.class);
        this.setFieldValByName("createdDt", now, metaObject);
        this.setFieldValByName("updatedDt", now, metaObject);
        this.setFieldValByName("deleted", Boolean.FALSE, metaObject);
        this.setFieldValByName("versionId", 1, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.debug("mybatis plus start update fill ....");
        //this.strictUpdateFill(metaObject, "updatedDt", () -> LocalDateTime.now(), LocalDateTime.class);
        this.setFieldValByName("updatedDt", LocalDateTime.now(), metaObject);
    }
}
